package org.aceor.mddal.gateway.datatype.impl;

import org.aceor.mddal.gateway.datatype.DataType;
import org.aceor.mddal.gateway.datatype.cal.AbstractCalculator;
import org.aceor.mddal.gateway.datatype.cal.Calculator;

import java.math.BigDecimal;

/**
 * Created by lxue on 16/4/17.
 */
public class BigDecimalType implements DataType<BigDecimal> {
    private Calculator calculator = new AbstractCalculator() {
        @Override
        public Object doAdd(Object value1, Object value2) {
            return ((BigDecimal)value1).add((BigDecimal) value2);
        }

        @Override
        public Object doSub(Object value1, Object value2) {
            return ((BigDecimal)value1).subtract((BigDecimal) value2);
        }

        @Override
        public Object doMultiply(Object value1, Object value2) {
            return ((BigDecimal)value1).multiply((BigDecimal) value2);
        }

        @Override
        public Object doDivide(Object value1, Object value2) {
            return ((BigDecimal)value1).divide((BigDecimal) value2);
        }
    };

    @Override
    public Calculator getCalculator() {
        return calculator;
    }

    @Override
    public int compare(BigDecimal o1, BigDecimal o2) {
        BigDecimal bigDecimal = o1.subtract(o2);
        return bigDecimal.compareTo(BigDecimal.ZERO);
    }
}
